home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / volume1a / readme.txt < prev   
Encoding:
Text File  |  1998-01-05  |  16.2 KB  |  412 lines

  1.              --------------------------------------------
  2.              Microsoft Volume Sample Project Readme File
  3.                             January l998
  4.              -------------------------------------------
  5.                     (c) Microsoft Corporation, 1997
  6.  
  7.  
  8. VOLUME.EXE is a self-extracting compressed file that contains a sample
  9. project demonstrating how to set the volume and microphone levels using
  10. Visual Basic.
  11.  
  12. After downloading and running the self-extracting file, the following files
  13. are copied to the Volume Level Project directory on your hard drive:
  14.  
  15.   Form1.frm    - the main form in the project
  16.   Module1.bas  - the module containing the function and type declarations.
  17.   Project1.vbp - the project file
  18.   Project1.vbw - the project workspace file
  19.   Readme.txt   - you are currently reading this file.
  20.  
  21. To set these microphone and volume levels from Visual Basic, use the
  22. following Windows API functions:
  23.  
  24.  - GlobalAlloc - allocates the specified number of bytes from the heap.
  25.  
  26.  - GlobalLock - locks a global memory object and returns a pointer to the
  27.    first byte of the object[ASCII 146]s memory block. The memory block
  28.    associated with a locked memory object cannot be moved or discarded.
  29.  
  30.  - GlobalFree - frees the specified global memory object and invalidates
  31.    its handle.
  32.  
  33.  - mixerClose - closes the specified mixer device.
  34.  
  35.  - mixerGetControlDetails - retrieves details about a single control
  36.    associated with an audio line.
  37.  
  38.  - mixerGetDevCaps - queries a specified mixer device to determine its
  39.    capabilities.
  40.  
  41.  - mixerGetID - retrieves the device identifier for a mixer device
  42.    associated with a specified device handle.
  43.  
  44.  - mixerGetLineControls - retrieves one or more controls associated with an
  45.    audio line.
  46.  
  47.  - mixerGetLineInfo - retrieves information about a specific line of a
  48.    mixer device.
  49.  
  50.  - mixerGetNumDevs - retrieves the number of mixer devices present in the
  51.    system.
  52.  
  53.  - mixerMessage - sends a custom mixer driver message directly to a mixer
  54.    driver.
  55.  
  56.  - mixerOpen - opens a specified mixer device and ensures that the device
  57.    will not be removed until the application closes the handle.
  58.  
  59.  - mixerSetControlDetails - sets properties of a single control associated
  60.    with an audio line.
  61.  
  62. The next section shows how to create a sample project that uses these
  63. functions to set the volume and microphone levels.
  64.  
  65. Create the Sample Project
  66. -------------------------
  67.  
  68. 1. Start a new Standard EXE project in Visual Basic. Form1 is created by
  69.    default.
  70.  
  71. 2. Add two command buttons, two text boxes, and two labels to Form1.
  72.  
  73. 3. Add a Module to the project by completing the following steps:
  74.  
  75.     - From the Project menu, click Add module. The Add Module dialog box
  76.       appears.
  77.  
  78.     - From the New tab, choose Module and click OK. A new module is added
  79.       to your project.
  80.  
  81. 4. Copy the following code to the Code window of Module1:
  82.  
  83.       Option Explicit
  84.  
  85.       Public Const MMSYSERR_NOERROR = 0
  86.       Public Const MAXPNAMELEN = 32
  87.       Public Const MIXER_LONG_NAME_CHARS = 64
  88.       Public Const MIXER_SHORT_NAME_CHARS = 16
  89.       Public Const MIXER_GETLINEINFOF_COMPONENTTYPE = &H3&
  90.       Public Const MIXER_GETCONTROLDETAILSF_VALUE = &H0&
  91.       Public Const MIXER_GETLINECONTROLSF_ONEBYTYPE = &H2&
  92.       Public Const MIXERLINE_COMPONENTTYPE_DST_FIRST = &H0&
  93.       Public Const MIXERLINE_COMPONENTTYPE_SRC_FIRST = &H1000&
  94.  
  95.       Public Const MIXERLINE_COMPONENTTYPE_DST_SPEAKERS = _
  96.                      (MIXERLINE_COMPONENTTYPE_DST_FIRST + 4)
  97.  
  98.       Public Const MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE = _
  99.                      (MIXERLINE_COMPONENTTYPE_SRC_FIRST + 3)
  100.  
  101.       Public Const MIXERLINE_COMPONENTTYPE_SRC_LINE = _
  102.                      (MIXERLINE_COMPONENTTYPE_SRC_FIRST + 2)
  103.  
  104.       Public Const MIXERCONTROL_CT_CLASS_FADER = &H50000000
  105.       Public Const MIXERCONTROL_CT_UNITS_UNSIGNED = &H30000
  106.  
  107.       Public Const MIXERCONTROL_CONTROLTYPE_FADER = _
  108.                      (MIXERCONTROL_CT_CLASS_FADER Or _
  109.                      MIXERCONTROL_CT_UNITS_UNSIGNED)
  110.  
  111.       Public Const MIXERCONTROL_CONTROLTYPE_VOLUME = _
  112.                      (MIXERCONTROL_CONTROLTYPE_FADER + 1)
  113.  
  114.       Declare Function mixerClose Lib "winmm.dll" _
  115.                      (ByVal hmx As Long) As Long
  116.  
  117.       Declare Function mixerGetControlDetails Lib "winmm.dll" _
  118.                      Alias "mixerGetControlDetailsA" _
  119.                      (ByVal hmxobj As Long, _
  120.                      pmxcd As MIXERCONTROLDETAILS, _
  121.                      ByVal fdwDetails As Long) As Long
  122.  
  123.       Declare Function mixerGetDevCaps Lib "winmm.dll" _
  124.                      Alias "mixerGetDevCapsA" _
  125.                      (ByVal uMxId As Long, _
  126.                      ByVal pmxcaps As MIXERCAPS, _
  127.                      ByVal cbmxcaps As Long) As Long
  128.  
  129.       Declare Function mixerGetID Lib "winmm.dll" _
  130.                      (ByVal hmxobj As Long, _
  131.                      pumxID As Long, _
  132.                      ByVal fdwId As Long) As Long
  133.  
  134.       Declare Function mixerGetLineControls Lib "winmm.dll" _
  135.                      Alias "mixerGetLineControlsA" _
  136.                      (ByVal hmxobj As Long, _
  137.                      pmxlc As MIXERLINECONTROLS, _
  138.                      ByVal fdwControls As Long) As Long
  139.  
  140.       Declare Function mixerGetLineInfo Lib "winmm.dll" _
  141.                      Alias "mixerGetLineInfoA" _
  142.                      (ByVal hmxobj As Long, _
  143.                      pmxl As MIXERLINE, _
  144.                      ByVal fdwInfo As Long) As Long
  145.  
  146.       Declare Function mixerGetNumDevs Lib "winmm.dll" () As Long
  147.  
  148.       Declare Function mixerMessage Lib "winmm.dll" _
  149.                      (ByVal hmx As Long, _
  150.                      ByVal uMsg As Long, _
  151.                      ByVal dwParam1 As Long, _
  152.                      ByVal dwParam2 As Long) As Long
  153.  
  154.       Declare Function mixerOpen Lib "winmm.dll" _
  155.                      (phmx As Long, _
  156.                      ByVal uMxId As Long, _
  157.                      ByVal dwCallback As Long, _
  158.                      ByVal dwInstance As Long, _
  159.                      ByVal fdwOpen As Long) As Long
  160.  
  161.       Declare Function mixerSetControlDetails Lib "winmm.dll" _
  162.                      (ByVal hmxobj As Long, _
  163.                      pmxcd As MIXERCONTROLDETAILS, _
  164.                      ByVal fdwDetails As Long) As Long
  165.  
  166.       Declare Sub CopyStructFromPtr Lib "kernel32" _
  167.                      Alias "RtlMoveMemory" _
  168.                      (struct As Any, _
  169.                      ByVal ptr As Long, _
  170.                      ByVal cb As Long)
  171.  
  172.       Declare Sub CopyPtrFromStruct Lib "kernel32" _
  173.                      Alias "RtlMoveMemory" _
  174.                      (ByVal ptr As Long, _
  175.                      struct As Any, _
  176.                      ByVal cb As Long)
  177.  
  178.       Declare Function GlobalAlloc Lib "kernel32" _
  179.                      (ByVal wFlags As Long, _
  180.                      ByVal dwBytes As Long) As Long
  181.  
  182.       Declare Function GlobalLock Lib "kernel32" _
  183.                      (ByVal hmem As Long) As Long
  184.  
  185.       Declare Function GlobalFree Lib "kernel32" _
  186.                      (ByVal hmem As Long) As Long
  187.  
  188.       Type MIXERCAPS
  189.          wMid As Integer                   '  manufacturer id
  190.          wPid As Integer                   '  product id
  191.          vDriverVersion As Long            '  version of the driver
  192.          szPname As String * MAXPNAMELEN   '  product name
  193.          fdwSupport As Long                '  misc. support bits
  194.          cDestinations As Long             '  count of destinations
  195.       End Type
  196.  
  197.       Type MIXERCONTROL
  198.          cbStruct As Long           '  size in Byte of MIXERCONTROL
  199.          dwControlID As Long        '  unique control id for mixer device
  200.          dwControlType As Long      '  MIXERCONTROL_CONTROLTYPE_xxx
  201.          fdwControl As Long         '  MIXERCONTROL_CONTROLF_xxx
  202.          cMultipleItems As Long     '  if MIXERCONTROL_CONTROLF_MULTIPLE
  203.                                     '  set
  204.          szShortName As String * MIXER_SHORT_NAME_CHARS  ' short name of
  205.                                                          ' control
  206.          szName As String * MIXER_LONG_NAME_CHARS        ' long name of
  207.                                                          ' control
  208.          lMinimum As Long           '  Minimum value
  209.          lMaximum As Long           '  Maximum value
  210.          reserved(10) As Long       '  reserved structure space
  211.          End Type
  212.  
  213.       Type MIXERCONTROLDETAILS
  214.          cbStruct As Long       '  size in Byte of MIXERCONTROLDETAILS
  215.          dwControlID As Long    '  control id to get/set details on
  216.          cChannels As Long      '  number of channels in paDetails array
  217.          item As Long           '  hwndOwner or cMultipleItems
  218.          cbDetails As Long      '  size of _one_ details_XX struct
  219.          paDetails As Long      '  pointer to array of details_XX structs
  220.       End Type
  221.  
  222.       Type MIXERCONTROLDETAILS_UNSIGNED
  223.          dwValue As Long        '  value of the control
  224.       End Type
  225.  
  226.       Type MIXERLINE
  227.          cbStruct As Long               '  size of MIXERLINE structure
  228.          dwDestination As Long          '  zero based destination index
  229.          dwSource As Long               '  zero based source index (if
  230.                                         '  source)
  231.          dwLineID As Long               '  unique line id for mixer device
  232.          fdwLine As Long                '  state/information about line
  233.          dwUser As Long                 '  driver specific information
  234.          dwComponentType As Long        '  component type line connects to
  235.          cChannels As Long              '  number of channels line supports
  236.          cConnections As Long           '  number of connections (possible)
  237.          cControls As Long              '  number of controls at this line
  238.          szShortName As String * MIXER_SHORT_NAME_CHARS
  239.          szName As String * MIXER_LONG_NAME_CHARS
  240.          dwType As Long
  241.          dwDeviceID As Long
  242.          wMid  As Integer
  243.          wPid As Integer
  244.          vDriverVersion As Long
  245.          szPname As String * MAXPNAMELEN
  246.       End Type
  247.  
  248.       Type MIXERLINECONTROLS
  249.          cbStruct As Long       '  size in Byte of MIXERLINECONTROLS
  250.          dwLineID As Long       '  line id (from MIXERLINE.dwLineID)
  251.                                 '  MIXER_GETLINECONTROLSF_ONEBYID or
  252.          dwControl As Long      '  MIXER_GETLINECONTROLSF_ONEBYTYPE
  253.          cControls As Long      '  count of controls pmxctrl points to
  254.          cbmxctrl As Long       '  size in Byte of _one_ MIXERCONTROL
  255.          pamxctrl As Long       '  pointer to first MIXERCONTROL array
  256.       End Type
  257.  
  258.       Function GetVolumeControl(ByVal hmixer As Long, _
  259.                               ByVal componentType As Long, _
  260.                               ByVal ctrlType As Long, _
  261.                               ByRef mxc As MIXERCONTROL) As Boolean
  262.  
  263.       ' This function attempts to obtain a mixer control.
  264.       ' Returns True if successful.
  265.          Dim mxlc As MIXERLINECONTROLS
  266.          Dim mxl As MIXERLINE
  267.          Dim hmem As Long
  268.          Dim rc As Long
  269.  
  270.          mxl.cbStruct = Len(mxl)
  271.          mxl.dwComponentType = componentType
  272.  
  273.          ' Obtain a line corresponding to the component type
  274.          rc = mixerGetLineInfo(hmixer, _
  275.                                mxl, _
  276.                                MIXER_GETLINEINFOF_COMPONENTTYPE)
  277.  
  278.          If (MMSYSERR_NOERROR = rc) Then
  279.              mxlc.cbStruct = Len(mxlc)
  280.              mxlc.dwLineID = mxl.dwLineID
  281.              mxlc.dwControl = ctrlType
  282.              mxlc.cControls = 1
  283.              mxlc.cbmxctrl = Len(mxc)
  284.  
  285.              ' Allocate a buffer for the control
  286.              hmem = GlobalAlloc(&H40, Len(mxc))
  287.              mxlc.pamxctrl = GlobalLock(hmem)
  288.              mxc.cbStruct = Len(mxc)
  289.  
  290.              ' Get the control
  291.              rc = mixerGetLineControls(hmixer, _
  292.                                        mxlc, _
  293.                                        MIXER_GETLINECONTROLSF_ONEBYTYPE)
  294.  
  295.              If (MMSYSERR_NOERROR = rc) Then
  296.                  GetVolumeControl = True
  297.  
  298.                  ' Copy the control into the destination structure
  299.                  CopyStructFromPtr mxc, mxlc.pamxctrl, Len(mxc)
  300.              Else
  301.                  GetVolumeControl = False
  302.              End If
  303.              GlobalFree (hmem)
  304.              Exit Function
  305.          End If
  306.  
  307.          GetVolumeControl = False
  308.       End Function
  309.  
  310.       Function SetVolumeControl(ByVal hmixer As Long, _
  311.                               mxc As MIXERCONTROL, _
  312.                               ByVal volume As Long) As Boolean
  313.       ' This function sets the value for a volume control.
  314.       ' Returns True if successful
  315.  
  316.          Dim mxcd As MIXERCONTROLDETAILS
  317.          Dim vol As MIXERCONTROLDETAILS_UNSIGNED
  318.  
  319.          mxcd.item = 0
  320.          mxcd.dwControlID = mxc.dwControlID
  321.          mxcd.cbStruct = Len(mxcd)
  322.          mxcd.cbDetails = Len(vol)
  323.  
  324.          ' Allocate a buffer for the control value buffer
  325.          hmem = GlobalAlloc(&H40, Len(vol))
  326.          mxcd.paDetails = GlobalLock(hmem)
  327.          mxcd.cChannels = 1
  328.          vol.dwValue = volume
  329.  
  330.          ' Copy the data into the control value buffer
  331.          CopyPtrFromStruct mxcd.paDetails, vol, Len(vol)
  332.  
  333.          ' Set the control value
  334.          rc = mixerSetControlDetails(hmixer, _
  335.                                     mxcd, _
  336.                                     MIXER_SETCONTROLDETAILSF_VALUE)
  337.  
  338.          GlobalFree (hmem)
  339.          If (MMSYSERR_NOERROR = rc) Then
  340.              SetVolumeControl = True
  341.          Else
  342.              SetVolumeControl = False
  343.          End If
  344.       End Function
  345.  
  346.  
  347. 5. Copy the following code to the Code window of Form1:
  348.  
  349.       Option Explicit
  350.  
  351.       Dim hmixer As Long          ' mixer handle
  352.       Dim volCtrl As MIXERCONTROL ' waveout volume control
  353.       Dim micCtrl As MIXERCONTROL ' microphone volume control
  354.       Dim rc As Long              ' return code
  355.       Dim ok As Boolean           ' boolean return code
  356.  
  357.       Private Sub Form_Load()
  358.       ' Open the mixer with deviceID 0.
  359.          rc = mixerOpen(hmixer, 0, 0, 0, 0)
  360.          If ((MMSYSERR_NOERROR <> rc)) Then
  361.              MsgBox "Couldn't open the mixer."
  362.              Exit Sub
  363.              End If
  364.  
  365.          ' Get the waveout volume control
  366.          ok = GetVolumeControl(hmixer, _
  367.                               MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, _
  368.                               MIXERCONTROL_CONTROLTYPE_VOLUME, _
  369.                               volCtrl)
  370.          If (ok = True) Then
  371.              ' If the function successfully gets the volume control,
  372.              ' the maximum and minimum values are specified by
  373.              ' lMaximum and lMinimum
  374.              Label1.Caption = volCtrl.lMinimum _
  375.                               & " to " _
  376.                               & volCtrl.lMaximum
  377.              End If
  378.  
  379.          ' Get the microphone volume control
  380.          ok = GetVolumeControl(hmixer, _
  381.                               MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE, _
  382.                               MIXERCONTROL_CONTROLTYPE_VOLUME, _
  383.                               micCtrl)
  384.          If (ok = True) Then
  385.              Label2.Caption = micCtrl.lMinimum _
  386.                               & " to " _
  387.                               & micCtrl.lMaximum
  388.              End If
  389.       End Sub
  390.  
  391.       Private Sub Command1_Click()
  392.          vol = CLng(Text1.Text)
  393.          SetVolumeControl hmixer, volCtrl, vol
  394.       End Sub
  395.  
  396.       Private Sub Command2_Click()
  397.          vol = CLng(Text2.Text)
  398.          SetVolumeControl hmixer, micCtrl, vol
  399.       End Sub
  400.  
  401. 5. On the Run menu, click Start or press the F5 key to start the program.
  402.    Minimize the Visual Basic IDE so all that appears on your display is
  403.    Form1. Maximize the Volume Level program in the Task bar. Arrange
  404.    Form1 and the Volume Level program so both programs are visible.
  405.  
  406. 6. Enter a value between the minimum and maximum values shown in one or
  407.    both text boxes and click one of the command buttons in Form1. Note that
  408.    the corresponding slider in the Volume Level program is adjusted to your
  409.    specified level.
  410.  
  411. (c) Microsoft Corporation 1997, All Rights Reserved.
  412. Contributions by Arsenio Locsin, Microsoft Corporation